package org.deng.learn.basic.funcinterface;

import java.lang.invoke.SerializedLambda;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author 邓力宾
 * @date 2020/5/16 下午5:09
 * @desc
 */
public class Main<T extends User> {

    public <U> void callFunc(Func<U> fun) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
        //继承Serializable接口，lambda合成的类多出writeReplace方法，返回java.lang.invoke.SerializedLambda类，
        //该类的serializedLambda.getImplMethodName()方法可以返回方法名称
        Method writeReplaceMethod = fun.getClass().getDeclaredMethod("writeReplace");
        writeReplaceMethod.setAccessible(true);
        SerializedLambda obj = (SerializedLambda) writeReplaceMethod.invoke(fun);
        String impMethodName = obj.getImplMethodName();
        System.out.println(impMethodName); // getMsg
    }

    /**
     * 方法作为参数 测试
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public  void test1() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {

        //Func func = User::getMsg;
        callFunc(T::getMsg); //方法作为参数

    }

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
        Main<AUser> main = new Main<>();
        main.test1();
    }
}
